﻿#region  
//=====================================================
// 文件名: TestAsyncLoadAssetBundle                
// 创建者: #AuthorName#                
// 创建时间: #CreateTime#              	
// Email: #AuthorEmail#               
// 描述: 当前脚本的功能              
// 修改者列表: 修改者名字以及修改功能      
// 版权所有: #Copyright#                 
//======================================================
#endregion 



using FrameWork;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FrameWork.Reource;
using UnityEngine.UI;

public class TestAsyncLoadAssetBundle : MonoBehaviour
{
    GameObject prefabGo;
    Sprite Sprite;
    private GameObject GO;
    private const string Prefab_Test1 = "Assets/GameMain/Prefabs/Test1/Test1.prefab";
    private const string Prefab_Cat = "Assets/GameMain/Picture/cat.jpg";

    private void Awake()
    {
        AssetBundleManager.Instance.LoadAssetBundleConfig();
        ResourceManager.Instance.Init(this);
    }


    void OnloadFinish(string path,Object obj, params object[] objs)
    {
        prefabGo = obj as GameObject;
        GO = GameObject.Instantiate(prefabGo);

       

    }

    void OnloadSpriteFinish(string path, Object obj, params object[] objs)
    {
        Sprite = obj as Sprite;
        Image im = GameObject.Find("Image").GetComponent<Image>();
        im.sprite = Sprite;
        im.SetNativeSize();
    }

    public GameObject go1;
    public GameObject go2;
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.K))
        {
            ResourceManager.Instance.AsyncLoadResource<GameObject>(Prefab_Test1, OnloadFinish, LoadResPriority.RES_HIGHT, 0, "123", "456", "789");
            ResourceManager.Instance.AsyncLoadResource<Sprite>(Prefab_Cat, OnloadSpriteFinish, LoadResPriority.RES_HIGHT, 0, "123", "456", "789");
        }
        if (Input.GetKeyDown(KeyCode.Y))
        {
            ResourceManager.Instance.ReleaseResource(prefabGo,true);
            ResourceManager.Instance.ReleaseResource(Sprite,true);
            Image im = GameObject.Find("Image").GetComponent<Image>();
            im.sprite = null;
            Sprite = null;
            Destroy(GO);
            GO = null;
            prefabGo = null;
        }


        Vector3 V = (go2.transform.position - Camera.main.transform.position).normalized;
        go2.transform.eulerAngles = go2.transform.rotation.x * V;

    }
    private void OnApplicationQuit()
    {
#if UNITY_EDITOR
        ResourceManager.Instance.Clear();
        Resources.UnloadUnusedAssets();
        Debug.Log("Clear the editor resource.");
#endif
    }
}
